package com.tanguy.user.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.tanguy.user.R;

/**
 * Created by dab on 2017/10/17 0017 14:48
 * 渐变颜色
 */

public class CurveView extends View {

    private Paint mPaint;
    private Path mPath;
    private int mWidth;
    private int mHeight;
    private int startColor;
    private int endColor;

    public CurveView(Context context) {
        this(context, null);
    }

    public CurveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        mWidth = widthSize;
        mHeight = heightSize;
        setMeasuredDimension(widthSize, heightSize);
    }

    private void init() {
        mPaint = new Paint();
        startColor = ContextCompat.getColor(getContext(), R.color.color_ffffff);
        endColor = ContextCompat.getColor(getContext(), R.color.color_ffffff);
//        startColor = ContextCompat.getColor(getContext(), R.color.styles_shade_start);
//        endColor = ContextCompat.getColor(getContext(), R.color.styles_shade_end);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(10);
        mPath = new Path();
//        LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //设置渐变色区域
        LinearGradient shader = new LinearGradient(0, 0, getRight(), 0, startColor, endColor, Shader.TileMode.MIRROR);
        mPaint.setShader(shader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        mPath.reset();
        int left = 0;
        int top = mHeight;
        int right = mWidth;
//        Log.e("55555555", "onMeasure: " + left);
//        Log.e("55555555", "onMeasure: " + top);
//        Log.e("55555555", "onMeasure: " + right);
        mPath.moveTo(left, top);
        mPath.lineTo(right+3, top);
//        mPath.quadTo(left, top, (left+right)/2, top+200);
        mPath.quadTo((left + right) / 2, -mHeight, left - 3, top - 3);
        canvas.drawPath(mPath, mPaint);
    }
}
